package com.zhongshan.ga.mobile.resource;

import java.io.IOException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

import com.zhongshan.ga.logger.GenerateLog;


/**
 * 用于测试能否连接到服务器端
 * 
 * @author pc-hao
 * 
 */
public class TestResource extends ServerResource {
	
	
	@Override
	public Request getRequest() {
		// TODO Auto-generated method stub
		return super.getRequest();
	}
	
	@Get
	public Representation get(Representation representation) throws IOException {
		representation = test(representation);
		return representation;
	}

	@Post
	public Representation test(Representation representation)
			throws IOException {

		GenerateLog.outDebug("starting test......");
		System.out.println("wo shi jian ren");

		if (representation == null) {
			System.out.println(getRequest().getClientInfo().getAddress());
//			System.out.println(representation.getLocationRef().getHostDomain());
//			System.out.println(representation.getLocationRef().getHostIdentifier());
//			System.out.println(representation.getLocationRef().getHostPort());
			return new StringRepresentation("连接到服务器！但是没有提交数据哦！");
		}
		System.out.println(getRequest().getClientInfo().getAddress());
//		System.out.println(getClientInfo().getAddresses());
		System.out.println(getClientInfo().getUpstreamAddress());
		System.out.println("000"+representation.getChannel());
		System.out.println("..."+representation.getDisposition());
		System.out.println("123"+getRequest().getClientInfo().getPort());
		System.out.println("456"+getHostRef().getHostDomain());
		System.out.println("789"+getHostRef().getIdentifier());
		
//		System.out.println(representation.getLocationRef().getHostIdentifier());
//		System.out.println(representation.getLocationRef().getHostPort());
		JSONObject json = new JSONObject();

		json.put("sign", "成功连接到服务器！");
		json.put("message", "亲，你发送过来的消息：" + representation.getText());
		json.put("type", "消息的数据类型：" + representation.getMediaType());
		json.put("number", "8");
		json.put("time", "8");

		System.out.println("test-->" + json.toString());

		GenerateLog.outDebug("ending test......");

		return new StringRepresentation(json.toString());
	}
}
